package com.zuipin.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.ProductDetail;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Jsons;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductDetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商品详情Controller
 *
 * @ClassName ProductDetailController
 * @author wangcongyi
 * @date 2017年5月2日 上午10:04:10
 */
@Controller
@RequestMapping(value = "/productDetail")
@Api(value = "商品详情", description = "商品详情-wangcongyi")
public class ProductDetailController {
	
	@Resource
	private IProductDetailService productDetailService;
	
	@Jsons({ @Json(type = ProductDetail.class, include = "id,proExtId,sysId,device,content"), })
	@RequestMapping(value = "/selectProDetail", method = RequestMethod.GET)
	@ApiOperation(value = "查找单个商品详情", notes = "查找单个商品详情")
	@Valids({ @Valid(name = "productDetail.proExtId", required = true), @Valid(name = "productDetail.sysId", required = true),
			@Valid(name = "productDetail.device", required = true, regex = "PC|WAP"), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "productDetail.proExtId", value = "商品拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.device", value = "终端,PC|WAP", required = true, dataType = "string", paramType = "query"), })
	public Result<?> selectProDetail(@Inject @ApiIgnore ProductDetail productDetail) {
		Result<ProductDetail> result = new Result<ProductDetail>();
		result.setData(productDetailService.selectProDetail(productDetail));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateOrCreateProDetail", method = RequestMethod.POST)
	@ApiOperation(value = "新增或更新商品详情(后台)", notes = "新增或更新商品详情(后台)")
	@Valids({ @Valid(name = "productDetail.id", minValue = 1, maxValue = 999999999), @Valid(name = "productDetail.proExtId", required = true, minValue = 1, maxValue = 999999999),
			@Valid(name = "productDetail.sysId", required = true, maxValue = 9), @Valid(name = "productDetail.device", required = true, regex = "PC|WAP"),
			@Valid(name = "productDetail.content", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "productDetail.id", value = "ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.proExtId", value = "商品拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.device", value = "终端,PC|WAP", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "productDetail.content", value = "详情内容", required = true, dataType = "string", paramType = "query") })
	public Result<?> updateOrCreateProDetail(@Inject @ApiIgnore ProductDetail productDetail) {
		Result<Integer> result = new Result<>();
		boolean flag = false;
		// 判断ID是否为空
		if (null != productDetail.getId()) {
			flag = productDetailService.updateByPrimaryKeySelective(productDetail);
			if (flag) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("商品详情更新成功！");
			} else {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("商品详情更新失败！");
			}
		} else {
			flag = productDetailService.insertSelective(productDetail);
			if (flag) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("商品详情添加成功！");
			} else {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("商品详情添加失败！");
			}
		}
		return result;
	}
	
}
